package com.applet.message.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

@Component
public class AliOSSUtil {
    private final static Logger logger = LoggerFactory.getLogger(AliOSSUtil.class);
    @Value("${oss.accessKeyId:''}")
    private String ACCESS_KEY_ID;
    @Value("${oss.accessKeySecrect:''}")
    private String ACCESS_KEY_SECRET;

    /**
     * @param file 本地文件
     * @param dir  bucket目录
     * @return 访问地址
     */
    public String uploadFile(String endPoint, String bucketName, File file, String dir) {
        if (file == null) {
            return null;
        }
        String filePath = dir + file.getName();
        OSSClient ossClient = new OSSClient(endPoint, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        try {
            PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, filePath, file));
            if (null != result) {
                return filePath;
            } else {
                return null;
            }
        } catch (OSSException | ClientException oe) {
            logger.error("picture upload fail:", oe);
            oe.printStackTrace();
            return null;
        } finally {
            ossClient.shutdown();
        }
    }
    public static File compressImg(File in_file){


            String image_name = in_file.getName();


            BufferedImage bi = null;
            try {
                bi = ImageIO.read(in_file);
            }catch(Exception e) {
                e.printStackTrace();
            }
            int in_width  = bi.getWidth();
            int in_height = bi.getHeight();
            int in_minx   = bi.getMinX();
            int in_miny   = bi.getMinY();
            int type = bi.getType();
            int out_width = in_width;
            int out_height = in_height;
            int multiple = 1;

            //具体的值可调,
            while(out_width * out_height > 5000000){
                out_width = out_width/2;
                out_height = out_height/2;
                multiple = multiple * 2;
            }
            BufferedImage out_image_martrix = new            BufferedImage(out_width, out_height, type);
            for(int i=0;i<out_width;i++) {
                for(int j =0;j<out_height;j++) {
                    int pixel =bi.getRGB(i*multiple+in_minx, j*multiple+in_miny);
                    out_image_martrix.setRGB(i, j, pixel);
                }
            }
        File out_file=new File("_compress"+image_name);
            try{

              //  after_image_path = after_image_path + image_name + ".jpg";
               ImageIO.write(out_image_martrix,"jpg", out_file);
                bi = null;
                out_image_martrix = null;
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                return out_file;
            }
        }

}
